<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/11
 * Time: 12:35
 */

namespace app\index\controller;

use think\Controller;

use think\Db;

use Qiniu\Storage\UploadManager;

use Qiniu\Auth;

use Qiniu;

use app\comm\controller\WebLogController;

    class UserRelateInfo extends Controller{
    /**
     * @Author  lignyun
     * @Desc    会员积分记录
     */
    public function my_point(){
        //会员id
        $uid = session('_user_uid_.uid');
        $where_user['uid'] = ['eq',$uid];
        $member_arr = db('user')->where($where_user)->field('uid,level_point,pay_points')->find();
        $log_arr = db('user_point_log')->where($where_user)->order('id desc')->select();
        $this->assign('member',$member_arr);
        $this->assign('log_arr',$log_arr);
        $this->assign('title','我的积分');
        return  $this->fetch('my_point');
    }

    /**
     * @Author  lignyun
     * @Desc    门店消费排名
     */
    public function store_consume_rank(){
        $uid = session('_user_uid_.uid');
        $member_arr = db::name('user')->where('uid',$uid)->field('uid,realname,nickname,total_amount')->find();
        $sql = "select a.title,IFNULL(b.amount,0) as amount,a.id from ys_store as a left join (select sum(final_amount) as amount,store_id from ys_order where uid=".$uid." group by store_id) as b on a.id=b.store_id where a.is_deleted=0 order by b.amount desc ";
        $list_arr = Db::query($sql);
        $this->assign('list',$list_arr);
        $this->assign('member',$member_arr);
        return $this->fetch('store_consume_rank');
    }

    /**
     * @Author  lignyun
     * @Desc    我的订单
     */
    public function my_order(){
        $uid = session('_user_uid_.uid');
        //订单状态,0待支付，1待发货，2已确认，3商家已发货，4已收货，5申请退货，6退货完成，7订单完成，8订单已取消（超过30分钟未支付就置为取消状态）
        //  0,1,2~3 ,7并且评价
        //0:待支付  1：待发货  2：待收货  3：待评价
        $status = input('status');
//        halt(input('param.'));
//        halt(input('status'));
        if(empty($status)){
            $status = 0;
        }
        //待收货
        $where_order = " where a.uid=".$uid;
        if($status == 2){
            $where_order .= " and a.order_status>1 and a.order_status<4";
        }else{
            $where_order .= " and a.order_status=".$status;
        }
//        halt($where_order);
        $sql = "select a.id, a.uid,a.final_amount,a.order_sn,a.order_status,a.pay_time,a.addtime,d.logo,d.name from ys_order as a left join (select * from ys_order_details group by order_id) as b on a.id=b.order_id left join ys_store_goods_stock as c on c.id=b.goods_id left join ys_goods as d on d.id=c.goods_id ".$where_order;
        $list_arr = Db::query($sql);
        $this->assign('list',$list_arr);
        $this->assign('status',$status);
        return $this->fetch();
    }

    /**
    * @Author  lingyun
    * @Desc    订单详情
    */
    public function order_detail(){
        $id = input('id');
        $where_order['id'] = ['eq',$id];
        $where_order_detail['order_id'] = ['eq',$id];
        $order = db('order')->where($where_order)->find();
        $order_detail = db('view_myorder_detail')->where($where_order_detail)->select();
        $this->assign('order',$order);
        $this->assign('order_detail',$order_detail);
        return $this->fetch('order_detail');
    }

        /**
         * @Author  lingyun
         * @Desc    提交的订单重新发起支付
         */
    public function pay_order(){
        $id = input('order_id');        //订单id
    }

    /**
     * @Author  lingyun
     * @Desc    取消订单
     */
    public function cancel_order(){
        $id = input('order_id');      //取消订单
        halt($id);
        $where_order['id'] = ['eq',$id];
        $order = db('order')->where($where_order)->find();
        $where_order['id'] = ['eq',$id];        //修改的订单id
        $update_data['order_status'] = 8;
        //未支付，不退款
        if($order['pay_status'] == 0){
            $res = db('order')->where('id',$id)->update($update_data);
        }else{
            $member = db('user')->where('uid',$order['uid'])->field('uid,user_money,realname')->find();
            $data = [
                'uid'=>$order['uid'],
                'order_id'=>$order['id'],
                'order_sn'=>$order['order_sn'],
                'amount'=>$order['final_amount'],
                'cur_amount'=>$order['final_amount']+$member['user_money'],
                'sign'=>1,
                'chargetype'=>3,
                'charge_cn'=>'取消订单退款',
                'addtime'=>time()
            ];
            $res = db('user_wallet_log')->data($data)->insert();
            db('user')->where('uid',$order['uid'])->setInc('user_money',$order['final_amount']);
            $res = db('order')->where('id',$id)->update($update_data);
        }
        $res!==false?$this->success('取消成功',''):$this->error('取消失败','');
    }

        /**
         * @Author  lingyun
         * @Desc    订单中的商品评价
         */
    public function order_recommend(){
        $request = request();
        if($request->isPost()){
            //七牛云配置
            $qiniu = config('qiniu');
            $name=time().session('_user_uid_.uid').$_FILES['images']['name'];             //文件名称
            $filePath=$_FILES['images']['tmp_name'];     //文件路径
            $type=$_FILES['images']['type'];             //文件类型

            $upManager = new UploadManager();
            $auth = new Auth($qiniu['accesskey'], $qiniu['secretkey']);
            $token = $auth->uploadToken($qiniu['bucket']);
            list($ret, $error) = $upManager->putFile($token,$name,$filePath,null,$type,false);
            if(!empty($error)){
                $this->error('图片上传失败,请稍后再试！',url('UserRelateInfo/my_order'));
            }
            $post = input('post.');
            $post['is_anonymous'] = isset($post['is_anonymous'])?1:0;

            $data = [
                'goods_id'=>$post['goods_id'],
                'content'=>$post['content'],
                'is_show'=>0,
                'uid'=>session('_user_uid_.uid'),
                'images'=>$qiniu['backurl'].$ret['key'],
                'order_id'=>$post['order_id'],
                'is_deleted'=>0,
                'is_anonymous'=>$post['is_anonymous'],
                'store_id'=>$post['store_id'],
                'addtime'=>time()
            ];
            $res = db('goods_comment')->data($data)->insert();
            $res>0?$this->success('评价成功',url('UserRelateInfo/my_order')):$this->error('保存失败',url('UserRelateInfo/my_order'));
        }else{
            $goods_id = input('goods_id');
            $order_id = input('order_id');
            $store_id = input('store_id');
            $this->assign('goods_id',$goods_id);
            $this->assign('order_id',$order_id);
            $this->assign('store_id',$store_id);
            return $this->fetch('order_recommend');
        }
    }

    }
